package com.zyajac.springBootDemo.controller;


import com.alibaba.fastjson.JSONObject;
import com.zyajac.springBootDemo.entity.DTreeNode;
import com.zyajac.springBootDemo.entity.SimpleData;
import com.zyajac.springBootDemo.service.LoginService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.util.*;

@RestController
//集合了Controller 和 ResponseBody 注解
@RequestMapping("")
public class HelloController {


    @Resource(name = "LoginService")
    private LoginService loginService;

    @RequestMapping(value = "/hello", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> hello(String params){
        JSONObject jsonObject = JSONObject.parseObject(params);
        String parentID = jsonObject.getString("departID");
        Map<String , Object> result= new HashMap<>();
        result.put("code", "000");


        List<DTreeNode> dataTree = new ArrayList<>( );
        int count = 0;
        DecimalFormat decimalFormat = new DecimalFormat("000");

        for (SimpleData simpleData :
                SimpleData.getDataByPID(parentID)) {
            DTreeNode dTreeNode = new DTreeNode();
            dTreeNode.setTreeId(decimalFormat.format(count));
                dTreeNode.setTitle(simpleData.getDepartName());
                dTreeNode.setParentId("0");
                dTreeNode.setDepartId(simpleData.getDepartID());
                dataTree.add(dTreeNode);
        }
        result.put("dataTree" , dataTree);

        return result;
    }

    @RequestMapping("")
    @ResponseBody
    public Map<String  , Object> index(){
        String hello = "欢迎使用 SpringBoot" ;
        return Collections.singletonMap("hello" , hello);
    }

    @RequestMapping("/getUser")
    @ResponseBody
    public Map<String , Object> getUser(String params){
        return loginService.login(params);
    }
}
